

//
//  RwdVisitorView.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/9.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
import SnapKit

class RwdVisitorView: UIView {
    
    var loginClosure: (()->())?
    
    //懒加载创建一个cycleImageview视图
    lazy var cycleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //懒加载创建一个homeimageview视图
    lazy var homeImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //懒加载创建问一个遮罩
    lazy var maskImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //懒加载一个下方的label控件
    lazy var contentLabel:UILabel = {
        //创建一个label
        let label = UILabel()
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        return label
        
    }()
    //懒加载的方式创建'注册'和'登录按钮'
    //  注册按钮
    @objc private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置文本及文本颜色
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    //  登录按钮按钮
    @objc private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置文本及文本颜色
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    
    //重写init(frame)方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
//        startAnimation()
    }
    //xib实现时需要写的
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //添加控件
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(homeImageView)
        addSubview(contentLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.center)
            
        }
        //小房子约束
        homeImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(cycleImageView)
            
        }
        //挡板
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(cycleImageView)
            
        }
        //文字label
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(maskImageView.snp_bottom).offset(10)
            make.width.equalTo(240)
            
        }
        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(contentLabel.snp_trailing)
            make.top.equalTo(contentLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 35))
            
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel.snp_leading)
            make.top.equalTo(contentLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
    }
    
    func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2*M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //添加一个不同tabbar button 显示不同的visitor 页面的方法
    func updateVisitorInfos(message:String? , imageName:String?) {
        
        if let msg = message , imgName = imageName {
            
            contentLabel.text = msg
            homeImageView.image = UIImage(named: imgName)
            cycleImageView.hidden = true
            
        }else{
            cycleImageView.hidden = false
            startAnimation()
        }
        
    }
    
    func loginAction() {
        
        print("点击了登录")
        
        loginClosure!()
        
    }
    
    func registerAction() {
        print("点击了注册")
        loginClosure!()
        
    }
    
    
}
